package com.freeter.modules.gjs.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 邮件发送工具类
 * 支持Office 365邮件发送
 */
public class SendMailUtil {

    private static final Logger logger = LoggerFactory.getLogger(SendMailUtil.class);

    /**
     * 发送Office 365邮件
     * 
     * @param to 收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容（支持HTML格式）
     * @param from 发件人邮箱
     * @param password 发件人密码
     * @throws MessagingException 邮件发送异常
     */
    public static void send365Email(String to, String subject, String content, String from, String password) throws MessagingException {
        try {
            // 创建邮件发送器
            JavaMailSender mailSender = createOffice365MailSender(from, password);
            
            // 创建邮件消息
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            
            // 设置邮件基本信息
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true); // true表示支持HTML格式
            
            // 发送邮件
            mailSender.send(message);
            
            logger.info("邮件发送成功 - 收件人: {}, 主题: {}", to, subject);
            
        } catch (MessagingException e) {
            logger.error("邮件发送失败 - 收件人: {}, 主题: {}, 错误: {}", to, subject, e.getMessage());
            throw e;
        } catch (Exception e) {
            logger.error("邮件发送异常 - 收件人: {}, 主题: {}, 错误: {}", to, subject, e.getMessage());
            throw new MessagingException("邮件发送异常: " + e.getMessage(), e);
        }
    }

    /**
     * 创建Office 365邮件发送器
     * 
     * @param from 发件人邮箱
     * @param password 发件人密码
     * @return JavaMailSender实例
     */
    private static JavaMailSender createOffice365MailSender(String from, String password) {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        
        // 设置Office 365 SMTP服务器
        mailSender.setHost("smtp.office365.com");
        mailSender.setPort(587);
        mailSender.setUsername(from);
        mailSender.setPassword(password);
        
        // 设置邮件属性
        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.debug", "false"); // 生产环境建议设置为false
        props.put("mail.smtp.connectiontimeout", "10000");
        props.put("mail.smtp.timeout", "10000");
        props.put("mail.smtp.writetimeout", "10000");
        
        return mailSender;
    }

    /**
     * 发送简单文本邮件
     * 
     * @param to 收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容（纯文本）
     * @param from 发件人邮箱
     * @param password 发件人密码
     * @throws MessagingException 邮件发送异常
     */
    public static void sendTextEmail(String to, String subject, String content, String from, String password) throws MessagingException {
        try {
            // 创建邮件发送器
            JavaMailSender mailSender = createOffice365MailSender(from, password);
            
            // 创建邮件消息
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, false, "UTF-8");
            
            // 设置邮件基本信息
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, false); // false表示纯文本格式
            
            // 发送邮件
            mailSender.send(message);
            
            logger.info("文本邮件发送成功 - 收件人: {}, 主题: {}", to, subject);
            
        } catch (MessagingException e) {
            logger.error("文本邮件发送失败 - 收件人: {}, 主题: {}, 错误: {}", to, subject, e.getMessage());
            throw e;
        } catch (Exception e) {
            logger.error("文本邮件发送异常 - 收件人: {}, 主题: {}, 错误: {}", to, subject, e.getMessage());
            throw new MessagingException("文本邮件发送异常: " + e.getMessage(), e);
        }
    }

    /**
     * 批量发送邮件
     * 
     * @param recipients 收件人邮箱列表
     * @param subject 邮件主题
     * @param content 邮件内容（支持HTML格式）
     * @param from 发件人邮箱
     * @param password 发件人密码
     * @throws MessagingException 邮件发送异常
     */
    public static void sendBulkEmail(String[] recipients, String subject, String content, String from, String password) throws MessagingException {
        try {
            // 创建邮件发送器
            JavaMailSender mailSender = createOffice365MailSender(from, password);
            
            // 创建邮件消息
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            
            // 设置邮件基本信息
            helper.setFrom(from);
            helper.setTo(recipients);
            helper.setSubject(subject);
            helper.setText(content, true); // true表示支持HTML格式
            
            // 发送邮件
            mailSender.send(message);
            
            logger.info("批量邮件发送成功 - 收件人数量: {}, 主题: {}", recipients.length, subject);
            
        } catch (MessagingException e) {
            logger.error("批量邮件发送失败 - 收件人数量: {}, 主题: {}, 错误: {}", recipients.length, subject, e.getMessage());
            throw e;
        } catch (Exception e) {
            logger.error("批量邮件发送异常 - 收件人数量: {}, 主题: {}, 错误: {}", recipients.length, subject, e.getMessage());
            throw new MessagingException("批量邮件发送异常: " + e.getMessage(), e);
        }
    }
} 